const jwt = require('jsonwebtoken');
const key = 'my key'; //定义秘钥

// 生成token
function getToken(uid,avatar) {
  // 申明携带的载荷
  let playload = {
    uid: uid,
    avatar:avatar,
    exp: Date.now() / 1000 + 60*60
  }
  let token = jwt.sign(playload, key)
  return token
}

// 验证token
function verifytoken(token) {
  let res = {};
  jwt.verify(token, key, (err, decoded) => {
    // console.log(err)
    // console.log(decoded)
    if (err) {
      return false
    }
    res = decoded
  })
  return res
}

module.exports = { getToken, verifytoken };
